#include<iostream>

//实现加法（使用   函数模板实现）
template <typename T>
T add(T t1, T t2)
{  
   
    // 如果输入的是整  数，返回相加结果
    if (typeid(t1).name() == typeid(int).name())
    {
        return t1 + t2;
    }

 //  如果是两个字符串，将两个字符串中间加上字符并返回
   if (typeid(t1).name() == typeid(std::string).name())
   {
        T out;
        out += t1;
        out += ' ';
        out += t2;
        return out;
   }

}

//实现减法(使用同名函数实现)
int sub(int a, int b)
{
   return a - b;
}
std::string sub(std::string str1 , std::string str2)
{
   int pos = str1.find(str2);
   int n = str2.size();
   return str1.erase(pos, n);
}

//实现比较(不同函数实现)
int compare_int(int a ,int b)   //比较数字
{
     if(a>b){
        return a;
     }else{
        return b;
     }
}

char compare_char(std::string str)  //比较字符
{
     int max_size = 'a';//用于记录字符串中最大的ascii码
     for (auto temp : str)
     {
          if(temp > max_size){
               max_size = temp;
          }
     }
     return (char)max_size;
}

//实现排序
int* my_sort(int arr[], int len)
{
     int mid =6;
     while (mid > 1) {
          mid = mid / 2; 
          for (int i = 0; i < len - mid; ++i) {
               int end = i;
               int temp = arr[end + mid];
               while (end >= 0) {
                    if (temp < arr[end]) {
                         arr[end + mid] = arr[end];
                         end -= mid;
                    }
                    else {
                         break;
                    }
               }
               arr[end + mid] = temp;
          }
     }
     //按照任务要求再排序
     int *array = new int[len];
     int num1 = 0;
     int num2 = 1;
     for (int i = 0; i < len / 2; i++)
     {
          array[i] = arr[len-num1 - 1];
          num1 += 2;
          array[len - i - 1] = arr[len - num2 - 1];
          num2 += 2;
     }
     return array;
}

void print(int arr[],int len)
{
     std::cout << "***排列***" << std::endl;
     for (int i = 0; i < len; i++)
          std::cout << arr[i] << std::endl;
}
int main()
{
     int array[11] = {1, 2, 5, 3, 8, 9, 4, 6, 0, 7, 10};
     std::string a = "aaabbbc";
     std::string b = "bbb";
     int c = 10;
     int d = 20;
     //加法调用
     std::cout << "add(a, b) = " << add(a, b) << std::endl; // 字符串
     std::cout << "add(c, d) = " << add(c, d) << std::endl; // 整形
     // 减法调用
     std::cout << "sub(a, b) = " << sub(a, b) << std::endl; // 字符串
     std::cout << "sub(c, d) = " << sub(c, d) << std::endl; // 整形
     // 比较调用
     std::cout << "compare_char(a) = " << compare_char(a) << std::endl;     // 字符串
     std::cout << "compare_int(c, d) = " << compare_int(c, d) << std::endl; // 整形
     // 排列调用
     int *arr = new int(sizeof(array) / sizeof(array[0]));
     arr = my_sort(array, sizeof(array) / sizeof(array[0]));
     print(arr, sizeof(array) / sizeof(array[0]));
}